pub mod app_handle;
pub mod app_log;
pub mod checksum;
pub mod db;

pub fn run() {
    println!("core is  running!");
}

//Linux / Unix 系统可以利用 kill(pid, 0)，发送 0 信号（不会真正杀进程），如果返回成功说明进程存在
#[cfg(unix)]
fn is_process_alive(pid: i32) -> bool {
    unsafe { libc::kill(pid, 0) == 0 }
}

#[cfg(windows)]
pub fn is_process_alive(pid: u32) -> bool {
    use windows_sys::Win32::Foundation::{CloseHandle, STILL_ACTIVE};
    use windows_sys::Win32::System::Threading::{
        GetExitCodeProcess, OpenProcess, PROCESS_QUERY_LIMITED_INFORMATION,
    };

    unsafe {
        let handle = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, 0, pid);
        if handle == std::ptr::null_mut() {
            return false;
        }

        let mut exit_code: u32 = 0;
        let ok = GetExitCodeProcess(handle, &mut exit_code);
        CloseHandle(handle);

        if ok == 0 {
            return false; // 调用失败，视为进程不存在
        }

        exit_code == STILL_ACTIVE as u32
    }
}
